<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class ProductModel extends Model
{
	// 确定链接表名
    protected $name = 'product';

    /*------------------------< 学校/专业 >------------------------*/

    /**
	 * [获取所有学校/专业]
     */
    public function getSchoolList(){
    	return DB::name('school_specialty')->select();
    }

    /**
	 * [添加学校/专业]
     */
    public function addSchool($param){
    	if(DB::name('school_specialty')->insert($param)){
    		return ['code'=>1,'msg'=>'添加成功'];
    	}else{
    		return ['code'=>0,'msg'=>'添加失败'];
    	}
    }

    /**
	 * [删除学校/专业]
     */
    public function delSchool($param){
    	if(DB::name('school_specialty')->where('id',$param)->delete()){
    		return ['code'=>1,'msg'=>'删除成功'];
    	}else{
    		return ['code'=>0,'msg'=>'删除失败'];
    	}
    }

    /**
	 * [编辑学校/专业]
     */
    public function editSchool($param){
    	try{
    		DB::name('school_specialty')->where('id',$param['id'])->update($param);
    		return ['code'=>1,'msg'=>'编辑成功'];
    	}catch(Exception $e){
    		return ['code'=>0,'msg'=>'编辑失败'.$e->getMessage()];
    	}
    }



    /*------------------------< 产品列表 >------------------------*/

    /**
	 * [获取信息]
	 */
	public function getInfoByWhere($where='',$whereOr='',$is_page=false,$offset=0,$limit=20,$order=''){
		if($is_page){
			return $this->where($where)->where($whereOr)->where($this->whereCansee())->limit($offset, $limit)->order($order)->select();
		}else{
			return $this->where($where)->where($whereOr)->where($this->whereCansee())->count();
		}
	}

    /**
     * [添加产品]
     */
    public function productAdd($param){
        try{
            $result = $this->validate('AddProValidate')->save($param);
            if(false === $result){
                return ['code'=>0,'msg'=>'添加失败:'.$this->getError()];
            }else{
                return ['code'=>1,'msg'=>'添加成功'];
            }
        }catch(Exception $e){
            return ['code'=>0,'msg'=>'添加失败'.$e->getMessage()];
        }
    }

    /**
     * [删除产品(伪删除)]
     */
    public function productDel($idArr){
        $this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->update(['delete_status'=>1]);
            }
            $this::commit();
            return ['code'=>1,'msg'=>'删除成功，已放入回收站'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"删除失败：".$e->getMessage()];
        }
    }

	/**
     * [删除产品(删除)]
     */
    public function productDelete($idArr){
        $this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->delete();
            }
            $this::commit();
            return ['code'=>1,'msg'=>'删除成功'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"删除失败：".$e->getMessage()];
        }
    }

	/**
     * [恢复产品]
     */
    public function productRecovery($idArr){
        $this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->update(['delete_status'=>0]);
            }
            $this::commit();
            return ['code'=>1,'msg'=>'恢复成功'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"恢复失败：".$e->getMessage()];
        }
    }

    /**
     * [编辑产品]
     */
    public function productEdit($param){
        try{
            $result = $this::validate('AddProValidate')->isUpdate(true)->save($param);
            if(false === $result){
                return ['code'=>0,'msg'=>'编辑失败:'.$this->getError()];
            }else{
                return ['code'=>1,'msg'=>'编辑成功'];
            }
        }catch(Exception $e){
            return ['code'=>0,'msg'=>'编辑失败'.$e->getMessage()];
        }
    }
}
